//积分详情
var server = require('../../utils/server');
var common = require('../../utils/common');
Page({
  data: {
    tel: "",
    lists: [],
    classHeight: '',
    page: 1,
    isFirstSearch: true,
    searchLoading: false, //"上拉加载"的变量，默认false，隐藏
    searchLoadingComplete: false, //“没有数据”的变量，默认false，隐藏
  },
  onLoad: function(options = {}) {
    this.setData({
      tel: options.tel
    })
  },
  onShow: function() {
    this.getLists();
    this.setData({
      classHeight: wx.getSystemInfoSync().windowHeight
    })
  },
  getLists() {
    server.postJSON(common.basePath + 'membercard/getscorelist', {
      "page": this.data.page,
      "mobile":this.data.tel
    }, res => {
      if (res.data.errno =='0') {
        this.setData({
          searchLoading: res.data.data.length < 30 ? false : true,
          searchLoadingComplete: res.data.data.length < 30 ? true : false,
          lists: res.data.data
        })
      } else {
        common.showErrorToast(res.data.errmsg);
      }
    })
  },
  toLower() {
    let that = this;
    if (that.data.searchLoading && !that.data.searchLoadingComplete) {
      that.data.isFirstSearch=false;
      that.setData({
        page: that.data.page + 1 //每次触发上拉事件，把searchPageNum+1
      });
      server.postJSON(common.basePath + 'membercard/getscorelist', {
        "page": this.data.page,
        "mobile": this.data.tel
      }, res => {
        if (res.data.errno == '0') {
          this.setData({
            searchLoading: res.data.data.length < 30 ? false : true,
            searchLoadingComplete: res.data.data.length < 30 ? true : false
          })
          this.loadMore(res);
        } else {
          common.showErrorToast(res.data.errmsg);
        }
      })
    }
  },
  loadMore(res) {
    if (res.data.data.length != 0) {
      let searchList = [];
      //如果isFromSearch是true从data中取出数据，否则先从原来的数据继续添加
      this.data.isFirstSearch ? searchList = res.data.data : searchList = this.data.lists.concat(res.data.data)
      this.setData({
        lists: searchList //获取数据数组
      });
      //没有数据了，把“没有数据”显示，把“上拉加载”隐藏
    } else {
      this.setData({
        searchLoadingComplete: true, //把“没有数据”设为true，显示
        searchLoading: false //把"上拉加载"的变量设为false，隐藏
      });
    }
  },
  goToShop(){
    wx.navigateTo({
      url: '../shop/productsListIndex',
    })
  }
})